package com.shihang.crm.test;

import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import com.shihang.android.script.AndroidUtil;
import com.shihang.android.script.CmdUtil;

public class Demo {
	public static void main(String[] args) {
		int width = 400;
		int height = 640;
		JFrame frame = new JFrame();
		frame.setTitle("Զ�̿���");
		frame.setSize(width, height+400);
		Container c = frame.getContentPane();
		c.setLayout(new FlowLayout());
		c.setBackground(new Color(248, 244, 178));
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});


		JLabel jLabel = new JLabel();
		frame.add(jLabel);
		ImageIcon imageIcon = new ImageIcon("E:/image/΢��ͼƬ_20190608110309.jpg");
		//System.out.println(imageIcon.getIconWidth());
		jLabel.setSize(width, height);
		jLabel.setOpaque(false);
		Image image = imageIcon.getImage();
		Image image2 = image.getScaledInstance(width, height, Image.SCALE_FAST);
		imageIcon = new ImageIcon(image2);
		jLabel.setIcon(imageIcon);
		

		Button button = new Button("��");
		button.setBounds(0, height+200, 100, 200);
		frame.add(button);
		button = new Button("home");
		button.setBounds(100, height+200, 100, 200);
		frame.add(button);
		button = new Button("back");
		button.setBounds(200, height+200, 100, 200);
		frame.add(button);
		

		jLabel.addMouseListener(new MyMouseAdapter());		
		

		new Thread() {
			public void run(){
				int i = 1;
				String filePath = "F:/phone/image/screen0.png";
				while (true) {
					i++;
					
					//ɾ����ͼƬ
					File o = new File(filePath);
					if(o.exists()){
						o.delete();
					}
					
					long time1 = System.currentTimeMillis();
					System.out.println(i+"screen");
							
					
					AndroidUtil.screenShot();
					long duration = System.currentTimeMillis() - time1;
					System.out.println(i+"screenOk:"+duration);
					filePath = "F:/phone/image/screen"+i+".png";
					new File("F:/phone/image/screen.png").renameTo(new File(filePath));
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					ImageIcon imageIcon = new ImageIcon(filePath);
					Image image = imageIcon.getImage();
					Image image2 = image.getScaledInstance(width, height, Image.SCALE_FAST);
					imageIcon = new ImageIcon(image2);
					jLabel.setIcon(imageIcon);					
					
				}
			};
		}.start();
	}
	
	static class MyMouseAdapter extends MouseAdapter{		

		long clickTime = 0;
		int x = 0;
		int y = 0;

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("����x:"+arg0.getX());
			System.out.println("����Y:"+arg0.getY());			
			clickTime = System.currentTimeMillis();
			x = arg0.getX();
			y = arg0.getY();
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("�ɿ�x:"+arg0.getX());
			System.out.println("�ɿ�Y:"+arg0.getY());
			int duration = (int) (System.currentTimeMillis() - clickTime);
			if(duration<300){
				AndroidUtil.click(arg0.getX()*3, arg0.getY()*3);
			} else {
				AndroidUtil.swipe(x*3, y*3, arg0.getX()*3, arg0.getY()*3, duration);
				
			}
		}
		
	}

}
